<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Sport extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Courses/SportModel','sport');
	}

	public function index()
	{
		$query = $this->db->query("SELECT id, name FROM nmp_sport_cat");
		$data['cat_list'] = $query->result_array();

		$this->load->view('sport_list',$data);
	}


	//获取数据
	public function get_list()
	{
		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$term['keyword'] = $this->input->get('keyword');
		$term['cat_id'] = $this->input->get('cat_id');

		$res = $this->sport->get_list_data($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//添加、编辑页
	public function sport_info()
	{
		$data['sport_id'] = intval($this->uri->segment(3));

		$query = $this->db->query("SELECT id, name FROM nmp_sport_cat");
		$data['cat_list'] = $query->result_array();
		
		//编辑
		if ($data['sport_id'] > 0) 
		{
			$res = $this->sport->get_sport_row($data['sport_id']);
		}

		$data['theme_img'] = isset($res['theme_img']) ? $res['theme_img'] : '' ;
		$data['name'] = isset($res['name']) ? $res['name'] : '' ;
		$data['cat_id'] = isset($res['cat_id']) ? $res['cat_id'] : '' ;
		$data['video'] = isset($res['video']) ? $res['video'] : '' ;
		$data['content'] = isset($res['content']) ? $res['content'] : '' ;
		$data['type'] = isset($res['type']) ? $res['type'] : 0 ;
		//图片上传地址
		$data['image_upload_url'] = $this->image_load_url;

		$this->load->view('sport_info', $data);
	}


	//保存入库
	public function insert_add()
	{
		$field = array('theme_img','name','cat_id','video','content','type');
		$arr = $this->input->post($field, TRUE);

		$sport_id = $this->input->post('sport_id');

		//添加
		if (empty($sport_id)) 
		{
			$res = $this->sport->inser_sport_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		//更新
		else
		{
			$res = $this->sport->updata_sport_data($arr,$sport_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}

		}

		echo json_encode($data);exit();
		
	}


	//分类页
	public function cat_list()
	{
		$query = $this->db->query("SELECT id, name FROM nmp_sport_cat");
		$data['cat_list'] = $query->result_array();

		$this->load->view('sport_cat', $data);
	}


	//添加分类
	public function add_cat()
	{
		$cat_name = $this->input->post('cat_name',TRUE);
		$cat_id = $this->input->post('cat_id', TRUE);

		$res = $this->sport->insert_sport_cat($cat_name,$cat_id);

		if ($res) 
		{
			$data['status'] = 200;
			$data['message'] = '操作成功！';

		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '操作失败！';
		}

		echo json_encode($data);exit();

	}


	//删除分类
	public function delete_cat()
	{

		$cat_id = $this->input->get('cat_id', true);

		$res = $this->sport->remove_cat($cat_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}


	//删除数据
	public function delete_sport()
	{
		$sport_id = $this->input->get('id', true);

		$res = $this->sport->remove_sport($sport_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}


}



?>